package com.kermel.andromeda.activities.alarm.list;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.BaseAdapter;
import android.widget.ListAdapter;

import com.kermel.andromeda.R;
import com.kermel.andromeda.activities.alarm.add.AddAlarmActivity;

public class MyAlarmsListActivity extends ListActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        setListAdapter(new MyAlarmsListAdapter(this));
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);

        menu.add(R.string.addAlarmMenuTitle).setIcon(android.R.drawable.ic_menu_add);
        
        return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        launchAddAlarmActivity();
        return true;
    }
    
    private void launchAddAlarmActivity() {
        Intent intent = new Intent(this, AddAlarmActivity.class);
        startActivityForResult(intent, AddAlarmActivity.RESULT_ALARM_ADDED);
    }
    
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        ListAdapter listAdapter = getListAdapter();
        if (listAdapter != null && listAdapter instanceof BaseAdapter) {
            BaseAdapter adapter = (BaseAdapter)listAdapter;
            adapter.notifyDataSetChanged();
        }
    }
}
